// by Xeno
// Commented/edited by Tyrghen

private ["_box_ownside", "_box_otherside","_other_side","_box_array", "_box","_first_elem"];

_box_array = [];
_box_array = d_player_ammobox_pos;

_fnc_spawn_content = {
	private ["_box","_side"];
	if (d_global_debug > 0) then { diag_log Format["(%1)[%2]: %3 %4",diag_tickTime,"playerammobox.sqf","Spawning content", _this]; };
	_box = _this select 0;
	_side = _this select 1;
	while {!isnull _box} do {
		[_box,_side] spawn x_weaponcargo;
		waituntil { sleep 1; ((player distance _box) < 100) };
		if (d_global_debug > 0) then { diag_log Format["(%1)[%2]: %3 %4",diag_tickTime,"playerammobox.sqf","Waiting for player to go away", getPos _box]; };
		// We wait that the player is far enough from the box before filling it.
		waituntil { sleep 5; ((player distance _box) > 500) };
		sleep 1200;
	};
};


// is either a position or an array with pos + direction
_first_elem = (_box_array) select 0;

_other_side = "EAST";
If (d_own_side == "WEST") then {_other_side = "EAST";} else {_other_side = "WEST";};

_box_ownside = [];
if (count _first_elem <= 2) then {
	{
		_box = d_the_base_box createVehicleLocal (_x select 0);
		_box setDir (_x select 1);
		_box setPos (_x select 0);
		_box setVariable ["ARTY_N_LOG_IGNORE_OBJECT",true];
		player reveal _box;
		_box allowdamage False;
		if (d_global_debug > 0) then { diag_log Format["(%1)[%2]: %3 %4",diag_tickTime,"playerammobox.sqf","Own Ammo Box created at", getPos _box]; };
		_box_ownside set [count _box_ownside, _box];
	} foreach _box_array;
} else {
	_box = d_the_base_box createVehicleLocal (_box_array select 0);
	_box setDir (_box_array select 1);
	_box setPos (_box_array select 0);
	_box setVariable ["ARTY_N_LOG_IGNORE_OBJECT",true];
	player reveal _box;
	_box allowdamage False;
	if (d_global_debug > 0) then { diag_log Format["(%1)[%2]: %3 %4",diag_tickTime,"playerammobox.sqf","Own Ammo Box created at", getPos _box]; };
	_box_ownside set [count _box_ownside, _box];
};

_box_otherside = [];
If (d_create_both_boxes) then {
	_box_array = d_second_ammobox_pos;
	// is either a position or an array with pos + direction
	_first_elem = (_box_array) select 0;
	if (count _first_elem <= 2) then {
		{
			_box = d_the_base_box createVehicleLocal (_x select 0);
			_box setDir (_x select 1);
			_box setPos (_x select 0);
			_box setVariable ["ARTY_N_LOG_IGNORE_OBJECT",true];
			player reveal _box;
			_box allowdamage False;
			if (d_global_debug > 0) then { diag_log Format["(%1)[%2]: %3 %4",diag_tickTime,"playerammobox.sqf","Own Ammo Box created at", getPos _box]; };
			_box_otherside set [count _box_otherside, _box];
		} foreach _box_array;
	} else {
		_box = d_the_base_box createVehicleLocal (_box_array select 0);
		_box setDir (_box_array select 1);
		_box setPos (_box_array select 0);
		_box setVariable ["ARTY_N_LOG_IGNORE_OBJECT",true];
		player reveal _box;
		_box allowdamage False;
		if (d_global_debug > 0) then { diag_log Format["(%1)[%2]: %3 %4",diag_tickTime,"playerammobox.sqf","Other Ammo Box created at", getPos _box]; };
		_box_otherside set [count _box_otherside, _box];
	};
};

// This will wait for the server script to create the weapon arrays. From there it will start to fill the boxes.
while {count((["d_weapon_array_enemy",[]] call XNetGetJIP)) <= 0} do {
	sleep 0.5;
};

{ [_x, d_own_side] spawn _fnc_spawn_content;
} foreach _box_ownside;

{ [_x, _other_side] spawn _fnc_spawn_content;
} foreach _box_otherside;
